package net.yunzhanyi.common.core.utils;

import net.yunzhanyi.common.core.constants.TimeConstants;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import static net.yunzhanyi.common.core.constants.TimeConstants.*;

/**
 * @author bestct
 * @date 2022/9/18
 * @type 类
 */
public class TimeUtils {

    /**
     * 计算两时间戳的时间间隔
     * @param beforeTimestamp
     * @param afterTimestamp
     * @return
     */
    public static String timestampToEasy(long beforeTimestamp, long afterTimestamp) {

        long separated = afterTimestamp - beforeTimestamp;

        long min = separated / nm;
        if (min == 0) {
            return "刚刚";
        }

        long hour = separated / nh;
        if (hour == 0) {
            return min + " 分钟前";
        }

        long day = separated / nd;
        if (day == 0) {
            return hour + " 小时前";
        } else if (day == 1) {
            return "昨天";
        }
        DateFormat dateFormat = new SimpleDateFormat(TimeConstants.YYYY_MM_DD);
        return dateFormat.format(new Date(beforeTimestamp));
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate)
    {
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }
}
